package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanKuFuJianSavePO;
import com.ct.application.shipping.model.po.ChuanYuanKuFuJianSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanKuFuJianVO;
import com.ct.application.shipping.service.ChuanYuanKuFuJianService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanKuFuJianMapper;
import com.ct.shipping.orm.ChuanYuanKuDongTaiDO;
import com.ct.shipping.orm.ChuanYuanKuFuJianDO;
import com.ct.shipping.orm.ChuanYuanKuFuJianDO;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 船员附件 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanKuFuJianServiceImpl extends BaseServiceImpl<ChuanYuanKuFuJianMapper, ChuanYuanKuFuJianDO>
        implements ChuanYuanKuFuJianService {

    /**
     * 查询职位分页列表
     */
    @Override
    public Page<ChuanYuanKuFuJianVO> findChuanYuanKuFuJian(ChuanYuanKuFuJianSearchPO searchPO) {
        LambdaQueryWrapper<ChuanYuanKuFuJianDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(searchPO.getChuanYuanId()),ChuanYuanKuFuJianDO::getChuanYuanId,searchPO.getChuanYuanId());
        lambdaQueryWrapper.orderByDesc(ChuanYuanKuFuJianDO::getCreateTime);
        Page<ChuanYuanKuFuJianDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanYuanKuFuJianDO> page1 = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(page1, ChuanYuanKuFuJianVO.class);
    }


    /**
     * 保存职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ChuanYuanKuFuJianVO saveChuanYuanKuFuJian(ChuanYuanKuFuJianSavePO savePO){
        savePO.setId(null); //新增不需要id
        ChuanYuanKuFuJianDO chuanYuanKuFuJianDO = tranferBo(savePO, ChuanYuanKuFuJianDO.class);
        if (Objects.nonNull(chuanYuanKuFuJianDO)){
            chuanYuanKuFuJianDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanYuanKuFuJianDO.setSystemName(UserThreadLocal.get().getSystemName());
            this.save(chuanYuanKuFuJianDO);
            return tranferBo(chuanYuanKuFuJianDO,ChuanYuanKuFuJianVO.class);
        }
        return null;
    }

    /**
     * 修改职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ChuanYuanKuFuJianVO updateChuanYuanKuFuJian(ChuanYuanKuFuJianSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        ChuanYuanKuFuJianDO chuanYuanKuFuJianDO = tranferBo(savePO, ChuanYuanKuFuJianDO.class);
        if (Objects.nonNull(chuanYuanKuFuJianDO)){
            this.updateById(chuanYuanKuFuJianDO);
            return tranferBo(chuanYuanKuFuJianDO,ChuanYuanKuFuJianVO.class);
        }
        return null;
    }


    /**
     * 删除职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanYuanKuFuJian(Long id){
        return this.removeById(id);
    }


    /**
     * 职位状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanYuanKuFuJianVO getChuanYuanKuFuJian(Long id){
        ChuanYuanKuFuJianDO chuanYuanKuFuJianDO = getById(id);
        return tranferBo(chuanYuanKuFuJianDO,ChuanYuanKuFuJianVO.class);
    }
}
